var pageNo=1;
var pageSize=10;
var flag=1;
$(document).ready(function(){
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
    showList();
});
//创建页码信息
function createPageInfo(list) {
    $("#startRow").text(list.startRow);
    $("#endRow").text(list.endRow);
    $("#totalPage").text(list.total);
    pageNum=list.pages;
    var temp = '';
    for (var i = 1; i < list.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageInfo").html(temp)
        .val(list.pageNum)
        .change(function () {
            $("#pageInfor").blur();
            if (flag !== 1) {
                return
            }
            pageNo = this.options[this.options.selectedIndex].value;
            showList();
        });
    $("#firstPage").click(function () {
        if (pageNo!= 1) {
            if (flag !== 1) {
                return
            }
            pageNo = 1;
            showList();
        }
    });
    $("#lastPage").click(function () {
        if (pageNo < pageNum) {
            if (flag !== 1) {
                return
            }
            pageNo = pageNum;
            showList();
        }
    });
    $("#nextPage").click(function () {
        if (pageNo < pageNum) {
            if (flag !== 1) {
                return
            }
            pageNo++;
            showList();
        }
    });
    $("#previousPage").click(function () {
        if (pageNo > 1) {
            if (flag !== 1) {
                return
            }
            pageNo--;
            showList();
        }
    });
    $("#pageSizeInfo").change(function () {
        $("#pageSizeInfo").blur();
        if (flag !== 1) {
            return
        }
        pageSize = this.options[this.options.selectedIndex].value;
        showList();
    });
}
//列表集
function showList(){
    if(flag!==1){
        return;
    }
    flag=0;
    ajax({
        url:"goodsSupplier/queryList",
        data:{
            pageNo:pageNo,
            pageSize:pageSize
        },
        success:function(data){
            
            flag=1;
            if(data.success){
                createPageInfo(data.data);
                document.getElementById("tbody").innerHTML=template("template",data.data);
            }else{
                layer.msg(data.message);
            }
        }
    });
}
//编辑或删除
function editSupplier(supplierId,isDelete){
    if(flag!==1){
        return;
    }
    flag=0;
    ajax({
        url:"goodsSupplier/addOrUpdate",
        data:{
            supplierId:supplierId,
            isDelete:isDelete
        },
        success:function(data){
            
            flag=1;
            layer.msg(data.message);
            if(data.success){
                showList();
            }
        }
    });
}
//编辑
$("#tbody").on("click",".delete",function () {
    var supplierId=$(this.parentNode.parentNode.parentNode).attr("data-id");
    editSupplier(supplierId,"98989801");
}).on("click",".edit",function (){
    var supplierId=$(this.parentNode.parentNode.parentNode).attr("data-id");
    var supplierMsg=$(this.parentNode.parentNode.parentNode).attr("data-msg");
    setStorJson("supplierMsg",supplierMsg);
    window.location.href="goodsSupplierAdd.html?supplierId="+supplierId;
});